{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import sympy as sp\n",
    "from chempy import Substance, Equilibrium, ReactionSystem\n",
    "from chempy.kinetics.ode import get_odesys\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "K1, K3, R1f, R3f = sp.symbols('K_1 K_3 R_1f R_3f')\n",
    "\n",
    "def mono_rev_single():\n",
    "    '''Reversible mechanism with one central complex (pg 36)\n",
    "    '''\n",
    "    E, A, P, X = map(Substance, 'EAPX')\n",
    "    equilibria = e1, e3 = [\n",
    "        Equilibrium({'E', 'A'}, {'X'}, param=K1),\n",
    "        Equilibrium({'X'}, {'E', 'P'}, param=K3)\n",
    "    ]\n",
    "    reactions = e1.as_reactions(kf=R1f) + e3.as_reactions(kf=R3f)\n",
    "    return ReactionSystem(reactions, (E, A, P, X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys = mono_rev_single()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(rsys.rxns[1].param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys = get_odesys(rsys, include_params=False, params=True)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert len(odesys.params) == 4\n",
    "odesys.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.get_jac()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c0 = {'E': 5, 'A': 2, 'P': 0, 'X': 0}\n",
    "params = {K1: 17, K3: 23, R1f: 63, R3f: 43}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.f_cb(42, [c0[k] for k in rsys.substances], [params[k] for k in odesys.params])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.names, odesys.param_names  # get_odesys or SymbolicSys could determine names from Symbol.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = odesys.integrate(42, c0, params, integrator='cvode')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result.plot(xscale='log')\n",
    "plt.gca().set_xlim([1e-6, plt.gca().get_xlim()[1]])\n",
    "_ = plt.legend(loc='best')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
